﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.Inventory;
using Microsoft.Practices.Composite.Wpf.Commands;
using Microsoft.Practices.Composite.Events;
using System.Windows;


namespace PJ.Presentation.Inventory
{
    public class InventoryItemProvider : PJ.Presentation.Inventory.IInventoryItemProvider
    {
        private IEventAggregator EventAggregator { get; set; }
        private IInventoryItemService Service {get; set;}

        public InventoryItemProvider(IInventoryItemService service, IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            Service = service;
            GetInventoryItemCommand = new DelegateCommand<string>(GetInventoryItem);
        }

        void GetInventoryItem(string id)
        {
            try
            {
                IInventoryItem InventoryItem = Service.FindByID(id) as IInventoryItem;
                EventAggregator.GetEvent<InventoryItemChangeEvent>().Publish(InventoryItem);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Erreur", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }

        public System.Windows.Input.ICommand GetInventoryItemCommand { get; set; }

        #region IDisposable Membres

        public void Dispose()
        {
//            if (Service != null)
//                Service.Dispose();
        }

        #endregion
    }
}
